package com.bite.captcha;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Date;
@RequestMapping("/admin")
@RestController
public class KaptchaController {
    private static final String  KAPTCHA_SESSION_KEY="KAPTCHA_SESSION_KEY";
    private static final String  KAPTCHA_SESSION_DATE="KAPTCHA_SESSION_DATE";
    private static final long  TIME_OUT=60*1000;
    @RequestMapping("/check")
    public boolean check(String inputCaptcha, HttpSession session){
        System.out.println("11111111111111");
        if(!StringUtils.hasLength(inputCaptcha)){
            return false;
        }
        System.out.println("22222222222222");
        String saveCaptcha = (String) session.getAttribute(KAPTCHA_SESSION_KEY);
        Date saveCaptchaDate = (Date) session.getAttribute(KAPTCHA_SESSION_DATE);
        System.out.println(inputCaptcha);
        System.out.println(saveCaptcha);
        if(inputCaptcha.equalsIgnoreCase(saveCaptcha)){
            if(saveCaptchaDate!=null||System.currentTimeMillis()-saveCaptchaDate.getTime()<TIME_OUT){
                System.out.println("++++++++++++");
                return true;
            }
        }
        System.out.println("33333333333333333");
        return false;
    }
}